package com.example.Lezedata.ui.activity.goods;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Build;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ReplacementSpan;
import android.text.style.StyleSpan;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.Lezedata.R;
import com.example.Lezedata.util.GBUtil;
import com.example.Lezedata.util.RadiusBackgroundSpan;

class GoodsNameViewModel {
    public String name;
    public String detail;
    public static GoodsNameViewModel init(String name , String detail){
        GoodsNameViewModel model = new GoodsNameViewModel();
        model.name = name;
        model.detail = detail;
        return model;
    }
}


public class GoodsNameView extends FrameLayout {

    public GoodsNameViewModel model;
    protected TextView textView;

    public GoodsNameView(Context context) {
        super(context);
        initView();
    }

    public GoodsNameView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public GoodsNameView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public GoodsNameView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    void initView(){
        textView = new TextView(getContext());
        textView.setTextColor(Color.WHITE);
        textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.START);
        textView.setLines(2);
        addView(textView);
    }

    public void setModel(GoodsNameViewModel model) {
        this.model = model;
        String str = model.name + " " + model.detail;
        SpannableString spannableString = new SpannableString(str);

        AbsoluteSizeSpan font1 = new AbsoluteSizeSpan(GBUtil.dpTo2px(getContext(),12));
        ForegroundColorSpan textColor = new ForegroundColorSpan(Color.WHITE);
        // 设置圆角背景
        RadiusBackgroundSpan backgroundSpan = new RadiusBackgroundSpan(Color.parseColor("#2CC6F3"),GBUtil.dpTo2px(getContext(),5),Color.WHITE);

        spannableString.setSpan(font1, 0, model.name.length() , Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(textColor, 0, model.name.length() , Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(backgroundSpan, 0, model.name.length() , Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        // 设置加粗
        StyleSpan styleSpan_B  = new StyleSpan(Typeface.BOLD);
        AbsoluteSizeSpan font2 = new AbsoluteSizeSpan(GBUtil.dpTo2px(getContext(),14));
        ForegroundColorSpan textColor1 = new ForegroundColorSpan(Color.parseColor("#4A4A4A"));

        spannableString.setSpan(font2, model.name.length() , spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(styleSpan_B, model.name.length() , spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(textColor1, model.name.length() , spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


        textView.setText(spannableString);
    }

    public void setModelForNameWithDetail(String name , String detail){
        GoodsNameViewModel goodsNameViewModel = new GoodsNameViewModel();
        goodsNameViewModel.name = name;
        goodsNameViewModel.detail = detail;
        setModel(goodsNameViewModel);
    }
}
